//
//  CXVisitorView.swift
//  XinaWeibo
//
//  Created by chenxuan on 2017/4/10.
//  Copyright © 2017年 chenxuan. All rights reserved.
//

import UIKit
import SnapKit

class CXVisitorView: UIView {
    // MARK: - 公有控件
    /// 注册按钮
    lazy var registerButton: UIButton = UIButton.cx_textButton(
        "注册",
        fontSize: 16,
        normalColor: UIColor.orange,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    
    /// 登录按钮
    lazy var loginButton: UIButton = UIButton.cx_textButton(
        "登录",
        fontSize: 16,
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    
    // MARK: - 私有控件
    /// 懒加载属性只有调用 UIKit 控件的指定构造函数，其他都需要使用类型
    /// 图像视图
    fileprivate lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮罩图像 - 不要使用 maskView
    fileprivate lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子
    fileprivate lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 提示标签
    fileprivate lazy var tipLabel: UILabel = UILabel.cx_label(
        withText: "关注一些人，回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGray)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 访客视图的信息字典 [imageName / message]
    /// 如果是首页 imageName == ""
    var visitorInfo:[String: String]? {
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                return
            }
            
            tipLabel.text = message
            
            // 3> 设置图像，首页不需要设置
            if imageName == "" {
                startAnimation()
                return
            }
            
            iconView.image = UIImage(named: imageName)
            
            // 其他控制器的访客视图不需要显示小房子/遮罩视图
            houseIconView.isHidden = true
            maskIconView.isHidden = true
           
        }
    }
    
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * Double.pi
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false
        
        iconView.layer.add(animation, forKey: nil)
    }
    

}

// MARK: - 设置界面
extension CXVisitorView {
    
    func setupUI() {
        // 设置背景颜色
        self.backgroundColor = UIColor.cx_color(withHex: 0xEDEDED)
        // 1. 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 文本居中
        tipLabel.textAlignment = .center
        // 设置间距
        let margin: CGFloat = 20.0
        
        // 2. 取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 1> 图像视图
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        // 2> 小房子
        houseIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        // 3> 提示标签
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.width.equalTo(236)
            make.top.equalTo(iconView.snp.bottom)
        }
        // 4> 注册按钮
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(margin)
            make.width.equalTo(100)
            make.left.equalTo(tipLabel)
        }
        // 5> 登录按钮
        loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(margin)
            make.width.equalTo(100)
            make.right.equalTo(tipLabel)
        }
        // 6> 遮罩图像
        maskIconView.snp.makeConstraints { (make) in
            make.width.top.equalTo(self)
            make.bottom.equalTo(loginButton)
        }
    }
    
    
    
}
